package com.augurit.wf.common.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.wf.common.entity.Jbpm4Variable;
import com.augurit.wf.mgmt.convert.Jbpm4VariableConvertor;
import com.augurit.wf.mgmt.web.form.Jbpm4VariableForm;

@Repository
public class Jbpm4VariableDao extends BaseDao<Jbpm4Variable,Long>{
	
	public List<Jbpm4Variable> getVariablesByProcInstId(String procInstId){
		List<Jbpm4Variable> list = null;
		if(procInstId != null && procInstId.trim().length()>0){
			String hql = "select vari from Jbpm4Execution exec,Jbpm4Variable vari,Jbpm4HistProcinst hisInst "
					 + " where hisInst.procInstId='" + procInstId + "' "
					 + " and exec.processInstanceId=hisInst.procInstDbId "
					 + " and  vari.executionDbid = exec.dbid";
			list = this.find(hql);
		}
		return list;
	}
	
	//修改
	public void update(Jbpm4VariableForm form){
		if(form != null && form.getDbid() != null){
			Jbpm4Variable entity = this.get(form.getDbid());
			Jbpm4VariableConvertor.convertToVo(form,entity);
			entity.setDbversion(entity.getDbversion() + 1L);
			super.save(entity);
		}
	}
	
	//根据dbid获取form对象
	public Jbpm4VariableForm getForm(Long dbid){
		Jbpm4VariableForm form = null;
		if(dbid != null){
			Jbpm4Variable entity = super.get(dbid);
			if(entity != null)
				form = new Jbpm4VariableForm(entity);
		}
		return form;
	}
}
